
/**
 * AppleIIGo
 * Nibble Disk Image class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include "NibbleDiskImage.h"
#include "FDIDiskImage.h"

NibbleDiskImage::NibbleDiskImage(char * diskImagePath) :
FloppyDiskImage(FLOPPYDISKTYPE_5_25, diskImagePath) {
}

UINT32 NibbleDiskImage::writeTrack(UINT8 * nibbleTrackData,
								   UINT32 nibbleTrackByteNum,
								   bool isSetTrackBitNum) {
	bool isSync = false;
	UINT32 trackBitNum = 0;
	UINT8 trackByte;
	
	for (UINT32 index = 0; index < nibbleTrackByteNum; index++) {
		UINT32 syncCount = 0;
		for (UINT32 syncIndex = 0;
			 syncIndex < nibbleTrackByteNum;
			 syncIndex++) {
			trackByte = nibbleTrackData[(index + syncIndex) %
										nibbleTrackByteNum];
			if (!(trackByte & 0x80))
				continue;
			if (trackByte != 0xff)
				break;
			syncCount++;
			if (syncCount >= NIBBLE_FF40_THRESHOLD) {
				isSync = true;
				break;
			}
		}
		
		trackByte = nibbleTrackData[index];
		if (!(trackByte & 0x80))
			continue;
		if (trackByte != 0xff)
			isSync = false;
		trackBitNum += isSync ? 10 : 8;
		
		if (!isSetTrackBitNum)
			writeNibble(trackByte, isSync ? 10 : 8);
	}
	
	return trackBitNum;
}
